package com.h3bpm.kumhosunny.sample.controller;

import OThinker.Common.Organization.Models.User;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Entity.Instance.Data.InstanceData;
import OThinker.H3.Entity.Notification.Notification;
import OThinker.H3.Portal.webservices.BPMService;
import OThinker.H3.Portal.webservices.Entity.BPMServiceResult;
import OThinker.H3.Portal.webservices.Entity.DataItemParam;
import OThinker.H3.Portal.webservices.impl.BPMServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/***
 * KS跟进记录
 */

@Controller
@RequestMapping({"/Portal/FollowUp"})
public class FollowUpController extends ControllerBase {

    private static Logger logger = LoggerFactory.getLogger(FollowUpController.class);

    private BPMService bpmService = new BPMServiceImpl();

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String getFunctionCode() {
        return null;
    }


    /***
     * KS继续跟进业务动作
     * @param instanceId
     * @return
     */
    // http://localhost:8081/Portal/FollowUp/continueFollowAction?instanceId=d55a7b07-7dd8-4354-8157-179df62fef28
    @RequestMapping("/continueFollowAction")
    @ResponseBody
    public String continueFollowUp(String instanceId) {

        String result="200";

        try {
            InstanceData instanceData=new InstanceData(getEngine(),instanceId, User.AdministratorID);
            HashMap<String, Object> valueTable=instanceData.getBizObject().getValueTable();

            //数据封装
            List<DataItemParam> dataParam = new ArrayList<>();
            dataParam.add(new DataItemParam("KsObjecttype",valueTable.get("KsObjecttype")));  //对象类型
            dataParam.add(new DataItemParam("KsCustomername",valueTable.get("KsCustomername")));  //客户名称
            dataParam.add(new DataItemParam("KsObjectname",valueTable.get("KsObjectname")));  //对象名称
            dataParam.add(new DataItemParam("KsObjectname-RD",valueTable.get("KsObjectname-RD")));  //对象名称

            dataParam.add(new DataItemParam("KsFollowperson",valueTable.get("KSPlanexecutor")));  //对象名称
            dataParam.add(new DataItemParam("KsFollowtime",valueTable.get("KsPlantime")));  //对象名称
            dataParam.add(new DataItemParam("KsTarget",valueTable.get("KsPlantarget")));  //对象名称
            dataParam.add(new DataItemParam("KsActionplan",valueTable.get("KsPlanaction")));  //对象名称


            //获取发起人
            User user= (User) getEngine().getOrganization().GetUnit(valueTable.get("CreatedBy").toString());
            //发起新的跟进表
            BPMServiceResult bpmServiceResult = bpmService.StartWorkflow("H3", "Authine",
                    "KsFollowup", user.getCode(), true, dataParam);

            Thread.sleep(300);
            DataTable dataTable=SqlUtil.doQuery(String.format("select  objectid from OT_workitem where instanceId = '%s' and ActivityCode = 'Activity13' and Participant = '%s' limit 1",bpmServiceResult.getInstanceId(),user.getObjectID()));
            String workItemId = dataTable.getRows().get(0).getString(0).toString();

            String pc_url="http://www.kumhosunny.cn:8099/Portal/template/ddhome.html?loginfrom=dingtalk&WorkItemID="+workItemId+"&time="+System.currentTimeMillis();
            String mobile_url="http://www.kumhosunny.cn:8099/Mobile/#/dingtalkmsg/"+workItemId+"?time="+System.currentTimeMillis();

            //发送消息通知用户
            //子流程数据
            InstanceData sonData=new InstanceData(getEngine(),bpmServiceResult.getInstanceId(),User.AdministratorID);

            Notification notification=new Notification(OThinker.H3.Entity.Notification.NotifyType.DingTalk,null,user.getObjectID(),
                    user.getDingTalkAccount(),null,0,bpmServiceResult.getInstanceId(),
                    ""+sonData.getInstanceContext().getInstanceName(),"您有一条流程待办,点击查看",null,mobile_url,pc_url,null);

            this.getEngine().getNotifier().Notify(notification);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    // http://localhost:8081/Portal/FollowUp/createCarSample?instanceId=d55a7b07-7dd8-4354-8157-179df62fef28
    /***
     * 生成新的汽车样品申请表
     * @param instanceId
     * @return
     */
    @RequestMapping("/createCarSample")
    @ResponseBody
    public String createNewCarSample(String instanceId){
        String result="200";

        try {
            InstanceData instanceData=new InstanceData(getEngine(),instanceId, User.AdministratorID);
            HashMap<String, Object> valueTable=instanceData.getBizObject().getValueTable();

            //数据封装
            List<DataItemParam> dataParam = new ArrayList<>();
            dataParam.add(new DataItemParam("customerName",valueTable.get("KsCustomername")));
            dataParam.add(new DataItemParam("fillPerson",valueTable.get("KsFollowupperson")));


            //获取发起人
            User user= (User) getEngine().getOrganization().GetUnit(valueTable.get("CreatedBy").toString());
            //发起新的跟进表
            BPMServiceResult bpmServiceResult = bpmService.StartWorkflow("H3", "Authine",
                    "sampleApplication", user.getCode(), false, dataParam);

            Thread.sleep(300);
            DataTable dataTable=SqlUtil.doQuery(String.format("select  objectid from OT_workitem where instanceId = '%s' and ActivityCode = 'Activity2' and Participant = '%s' limit 1",bpmServiceResult.getInstanceId(),user.getObjectID()));
            String workItemId = dataTable.getRows().get(0).getString(0).toString();

            String pc_url="http://www.kumhosunny.cn:8099/Portal/template/ddhome.html?loginfrom=dingtalk&WorkItemID="+workItemId+"&time="+System.currentTimeMillis();
            String mobile_url="http://www.kumhosunny.cn:8099/Mobile/#/dingtalkmsg/"+workItemId+"?time="+System.currentTimeMillis();

            //发送消息通知用户
            //子流程数据
            InstanceData sonData=new InstanceData(getEngine(),bpmServiceResult.getInstanceId(),User.AdministratorID);

            Notification notification=new Notification(OThinker.H3.Entity.Notification.NotifyType.DingTalk,null,user.getObjectID(),
                    user.getDingTalkAccount(),null,0,bpmServiceResult.getInstanceId(),
                    ""+sonData.getInstanceContext().getInstanceName(),"您有一条流程待办,点击查看",null,mobile_url,pc_url,null);

            this.getEngine().getNotifier().Notify(notification);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    // http://localhost:8081/Portal/FollowUp/createNoCarSample?instanceId=d55a7b07-7dd8-4354-8157-179df62fef28
    /***
     * 生成非汽车样品申请单
     * @param instanceId
     * @return
     */
    @RequestMapping("/createNoCarSample")
    @ResponseBody
    public String createNoCarSample(String instanceId){
        String result="200";
        try {
            InstanceData instanceData=new InstanceData(getEngine(),instanceId, User.AdministratorID);
            HashMap<String, Object> valueTable=instanceData.getBizObject().getValueTable();

            //数据封装
            List<DataItemParam> dataParam = new ArrayList<>();
            dataParam.add(new DataItemParam("customerName",valueTable.get("KsCustomername")));
            dataParam.add(new DataItemParam("fillPerson",valueTable.get("KsFollowupperson")));


            //获取发起人
            User user= (User) getEngine().getOrganization().GetUnit(valueTable.get("CreatedBy").toString());
            //发起新的跟进表
            BPMServiceResult bpmServiceResult = bpmService.StartWorkflow("H3", "Authine",
                    "unCarSampleAppliction", user.getCode(), false, dataParam);

            Thread.sleep(300);
            DataTable dataTable=SqlUtil.doQuery(String.format("select  objectid from OT_workitem where instanceId = '%s' and ActivityCode = 'Activity2' and Participant = '%s' limit 1",bpmServiceResult.getInstanceId(),user.getObjectID()));
            String workItemId = dataTable.getRows().get(0).getString(0).toString();

            String pc_url="http://www.kumhosunny.cn:8099/Portal/template/ddhome.html?loginfrom=dingtalk&WorkItemID="+workItemId+"&time="+System.currentTimeMillis();
            String mobile_url="http://www.kumhosunny.cn:8099/Mobile/#/dingtalkmsg/"+workItemId+"?time="+System.currentTimeMillis();

            //发送消息通知用户
            //子流程数据
            InstanceData sonData=new InstanceData(getEngine(),bpmServiceResult.getInstanceId(),User.AdministratorID);

            Notification notification=new Notification(OThinker.H3.Entity.Notification.NotifyType.DingTalk,null,user.getObjectID(),
                    user.getDingTalkAccount(),null,0,bpmServiceResult.getInstanceId(),
                    ""+sonData.getInstanceContext().getInstanceName(),"您有一条流程待办,点击查看",null,mobile_url,pc_url,null);

            this.getEngine().getNotifier().Notify(notification);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  result;

    }


}
